<template>
	<view class="container">
		<view class="blue_bg" />

		<view class="coupon_til">
			<view :class="{ 'active': active === 1 }" @click="onActive(1)">
				待使用
			</view>
			<view :class="{ 'active': active === 0 }" @click="onActive(0)">
				已使用
			</view>
			<view :class="{ 'active': active === -1 }" @click="onActive(-1)">
				已过期
			</view>
		</view>

		<view class="coupon_tab">
			<view v-for="(item,index) in filteredCoupons" :key="index" :class=" { 'coupon_item':true,
			'coupon_over': active === 0, 'coupon_item coupon_overdue': active === -1 } ">
				<view class="item_up">
					<view class="left">
						仅限于{{item.cheShop.name}}加油站使用
						<text>满{{item.full}}元可用</text>
					</view>
					<view class="right">
						￥<text :class=" { 'del':active === -1 } ">{{item.reduce}}</text>
					</view>
				</view>
				<view class="item_down">
					<view class="left">
						有效期至:{{item.startTime}}—{{item.endTime}}
					</view>
					<view class="right">
						去使用>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				active: 1, // 1:待使用, 0:已使用, -1:已过期
				userId: '',
				coupon: [], // 所有优惠券
				shop: [],
				filteredCoupons: [] // 筛选后的优惠券
			}
		},
		onLoad() {
			this.userId = uni.getStorageSync("user_id");
			this.getUserDetails();
		},
		methods: {
			onActive(e) {
				this.active = e;
				this.filterCoupons(); // 根据当前active值过滤优惠券
			},
			getUserDetails() {
				let that = this;
				this.$http({
					url: `/api/user/users/${that.userId}`,
					method: 'GET',
					success(res) {
						that.coupon = res.data.data.coupons;
						console.log(that.coupon);
						that.filterCoupons(); // 获取数据后进行过滤
					}
				})
			},
			filterCoupons() {
				const now = new Date().getTime(); // 获取当前时间戳
				this.filteredCoupons = this.coupon.filter(item => {
					const startTime = new Date(item.startTime).getTime();
					const endTime = new Date(item.endTime).getTime();

					// 根据active状态过滤优惠券
					if (this.active === 1) {
						// 未使用且有效期内的优惠券
						return item.state === 1 && now >= startTime && now <= endTime;
					} else if (this.active === 0) {
						// 已使用的优惠券
						return item.state === 2;
					} else if (this.active === -1) {
						// 已过期的优惠券
						return item.state === 3 || now > endTime;
					}
					return true; // 默认返回所有优惠券
				});
			}
		}
	}
</script>

<style>
	@import '@/static/images/ryx/all.css';

	.coupon_til {
		width: 100vw;
		box-sizing: border-box;
		padding: 20rpx 40rpx;
		font-size: 40rpx;
		color: #fff;
		background-color: #2679f5;
		display: flex;
		flex-flow: row nowrap;
		justify-content: space-between;
	}

	.active {
		border-bottom: #fff solid 2px;
	}

	.coupon_tab {
		width: 90vw;
		padding: 20rpx;
		margin-top: 30rpx;
		border-radius: 20rpx;
		background-color: #fff;
	}

	.coupon_item {
		height: 19vh;
		box-sizing: border-box;
		padding: 0 26rpx;
		margin: 20rpx;
		background-image: url('@/static/images/ryx/asset/bg_@2x.png');
		background-repeat: no-repeat;
		background-size: 100%;
		display: flex;
		flex-flow: column;
		color: #fff;
	}

	.coupon_over {
		background-image: none;
		background-color: #ffbb99;
	}

	.coupon_overdue {
		background-image: none;
		background-color: #bfbfbf;
	}

	.coupon_item .item_up {
		height: 80%;
		display: flex;
		flex-flow: row nowrap;
		justify-content: space-between;
		align-items: center;
	}

	.coupon_item .item_up .left {
		display: flex;
		flex-flow: column;
	}

	.coupon_item .item_up .left text {
		font-size: 40rpx;
		font-weight: bold;
	}

	.coupon_item .item_up .right text {
		font-size: 80rpx;
		font-weight: bold;
	}

	.coupon_item .item_down {
		height: 40%;
		border-top: #fff dashed 1rpx;
		display: flex;
		flex-flow: row nowrap;
		justify-content: space-between;
		align-items: center;
	}
</style>